/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.som;

import botobjects.SoMArmy;
import main.CommonMethods;
import main.Constants;
import org.jibble.pircbot.Colors;
import tools.UtopiaTime;
import utopiaintelpackage.AbstractIntel;

import java.util.*;

/**
 * This class represents a SoM SoMs have SoMArmy objects in it, if armies are actually out The class offers convenience methods
 * for adding and removing commonly used parameters
 *
 * 
 */
public class SoM extends AbstractIntel {
    private boolean isSelfKD = false;

    @SuppressWarnings("unchecked")
    public void addArmy(final SoMArmy army) {
        if (getParameter("armies") == null) {
            ArrayList<SoMArmy> list = new ArrayList<SoMArmy>();
            list.add(army);
            setParameter("armies", list);
        } else {
            ((List<SoMArmy>) getParameter("armies")).add(army);
        }
    }

    @SuppressWarnings("unchecked")
    public List<SoMArmy> getArmies() {
        if (getParameter("armies") == null)
            return new ArrayList<SoMArmy>();
        return (List<SoMArmy>) getParameter("armies");
    }

    public String getExportLine() {
        if (getParameter("export") == null)
            return "";
        return (String) getParameter("export");
    }

    @Override
    public List<String> getIRCOutput(final boolean shortVersion) {
        List<String> out = new LinkedList<String>();
        String adderPers = CommonMethods.getPersonality(getSavedBy());
        out.add(">> " + Colors.OLIVE + getName() + getKdloc() + Colors.NORMAL + " << " + Colors.OLIVE + getRace() + " "
                + getPersonality() + Colors.NORMAL
                + (adderPers.equals("Tactician") ? Colors.BOLD + " Tactician-" + Colors.NORMAL : " ") + "SoM added: "
                + UtopiaTime.compareTimes(getUpdated().getTime()) + " ago"
                + ("".equals(getSavedBy()) ? "" : " by " + Colors.NORMAL + "o" + Colors.RED + getSavedBy() + Colors.NORMAL + "o"));
        out.add(Colors.BLUE + "Net Defense at Home: " + Colors.OLIVE + getNetDefense() + " " + Colors.BLUE
                + "Net Offense at Home: " + Colors.OLIVE + getNetOffense());
        if (shortVersion) {
            out.add("Export:" + Colors.DARK_GREEN + " " + getExportLine());
        } else {
            for (SoMArmy army : getArmies()) {
                if (army.getArmyType() == Constants.SOM_ARMY_HOME && isSelfKD) {
                    out.add(Colors.BLUE + "Army home:" + Colors.BLUE + " Solds: " + Colors.OLIVE + army.getSoldiers() + Colors.BLUE
                            + " DS: " + Colors.OLIVE + army.getDspecs() + Colors.BLUE + " OS: " + Colors.OLIVE
                            + army.getOspecs() + Colors.BLUE + " Elites: " + Colors.OLIVE + army.getElites());
                } else if (army.getArmyType() == Constants.SOM_ARMY_OUT) {
                    out.add(Colors.BLUE + "Army #: " + Colors.OLIVE + army.getArmyno() + Colors.BLUE + " with " + Colors.RED
                            + army.getGenerals() + Colors.BLUE + " gens returns in: " + Colors.RED
                            + UtopiaTime.compareTimes(army.getReturnTime()) + Colors.BLUE + " Solds: " + Colors.OLIVE
                            + army.getSoldiers() + Colors.BLUE + " OS: " + Colors.OLIVE + army.getOspecs() + Colors.BLUE
                            + " Elites: " + Colors.OLIVE + army.getElites() + Colors.BLUE + " Land: " + Colors.OLIVE + army.getLand());
                } else if (army.getArmyType() == Constants.SOM_ARMY_TRAINING) {
                    out.add(Colors.BLUE + "In training:" + Colors.BLUE + " DS: " + Colors.OLIVE + army.getDspecs() + Colors.BLUE
                            + " OS: " + Colors.OLIVE + army.getOspecs() + Colors.BLUE + " Elites: " + Colors.OLIVE
                            + army.getElites() + Colors.BLUE + " Thieves: " + Colors.OLIVE + army.getThieves());
                }
            }
            if (!getExportLine().equals("")) {
                out.add("Export:" + Colors.DARK_GREEN + " " + getExportLine());
            }
        }
        return out;
    }

    public String getKdloc() {
        if (getParameter("kdloc") == null)
            return "";
        return (String) getParameter("kdloc");
    }

    public String getName() {
        if (getParameter("name") == null)
            return "";
        return (String) getParameter("name");
    }

    public int getNetDefense() {
        if (getParameter("netdef") == null)
            return 0;
        return (Integer) getParameter("netdef");
    }

    public int getNetOffense() {
        if (getParameter("netoff") == null)
            return 0;
        return (Integer) getParameter("netoff");
    }

    public String getPersonality() {
        if (getParameter("personality") == null)
            return "";
        return (String) getParameter("personality");
    }

    public String getRace() {
        if (getParameter("race") == null)
            return "";
        return (String) getParameter("race");
    }

    @Override
    public Map<String, Object> getRPCOutput(final int... args) {
        Map<String, Object> out = new HashMap<String, Object>();
        out.put("Name", getName());
        out.put("KD", getKdloc());
        out.put("Race", getRace());
        out.put("Personality", getPersonality());
        out.put("Ruler Name", getRulerName());
        out.put("Title", getTitle());
        out.put("Net Defense at Home", getNetDefense());
        Map<String, Map<String, Object>> armies = new HashMap<String, Map<String, Object>>();
        for (SoMArmy army : getArmies()) {
            String armyType = "";
            if (army.getArmyType() == Constants.SOM_ARMY_HOME) {
                armyType = "Army Home";
            } else if (army.getArmyType() == Constants.SOM_ARMY_TRAINING) {
                armyType = "Army Training";
            } else if (army.getArmyType() == Constants.SOM_ARMY_OUT) {
                armyType = "Army Out #" + army.getArmyno();
            }
            armies.put(armyType, army.getRPCOutput());
        }
        out.put("Armies", armies);
        out.put("Export Line", getExportLine());
        out.put("Saved By", getSavedBy());
        out.put("Added Time", getUpdated());
        return out;
    }

    public String getRulerName() {
        if (getParameter("rulername") == null)
            return "";
        return (String) getParameter("rulername");
    }

    public String getSavedBy() {
        if (getParameter("savedby") == null)
            return "";
        return (String) getParameter("savedby");
    }

    public String getTitle() {
        if (getParameter("title") == null)
            return "";
        return (String) getParameter("title");
    }

    @Override
    public String getTypeOfIntel() {
        return "SoM";
    }

    public Date getUpdated() {
        return (Date) getParameter("updated");
    }

    @Override
    public boolean isComplete() {
        return getParameter("name") != null && getParameter("kdloc") != null && getParameter("title") != null
                && getParameter("race") != null && getParameter("personality") != null && getParameter("rulername") != null
                && getParameter("updated") != null;
    }

    public void setExportLine(final String export) {
        setParameter("export", export);
    }

    public void setIsSelfKD(final boolean isSelfKD) {
        this.isSelfKD = isSelfKD;
    }

    public void setKdloc(final String kdloc) {
        setParameter("kdloc", kdloc);
    }

    public void setName(final String name) {
        setParameter("name", name);
    }

    public void setNetDefense(final int netdef) {
        setParameter("netdef", netdef);
    }

    public void setNetOffense(final int netoff) {
        setParameter("netoff", netoff);
    }

    public void setPersonality(final String personality) {
        setParameter("personality", personality);
    }

    public void setRace(final String race) {
        setParameter("race", race);
    }

    public void setRulerName(final String rulerName) {
        setParameter("rulername", rulerName);
    }

    public void setSavedBy(final String savedBy) {
        setParameter("savedby", savedBy);
    }

    public void setTitle(final String title) {
        setParameter("title", title);
    }

    public void setUpdated(final Date updated) {
        setParameter("updated", updated);
    }

}
